package com.runshu.runshuwulian.ui.activity;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.Editable;
import android.text.Html;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.Nullable;

import com.runshu.runshuwulian.R;
import com.runshu.runshuwulian.base.BaseActivity;
import com.runshu.runshuwulian.ui.viewhelper.LoginByPhoneViewHelper;
import com.runshu.runshuwulian.utils.MTextUtil;
import com.runshu.runshuwulian.utils.StatusBarUtil;
import com.runshu.runshuwulian.widget.TopTitleBar;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Demo class
 *
 * @author yu
 * @date 2020-05-09
 * @decs：手机号验证码登录
 */

public class LoginByPhoneActivity extends BaseActivity {
    @BindView(R.id.titlebar)
    TopTitleBar titlebar;
    @BindView(R.id.et_phone)
    EditText et_phone;
    @BindView(R.id.et_yzm)
    EditText et_yzm;
    @BindView(R.id.tv_hqyzm)
    TextView tv_hqyzm;
    @BindView(R.id.bt_login)
    View bt_login;
    @BindView(R.id.bt_clear_phonestr)
    ImageView btClearPhonestr;
    @BindView(R.id.bt_clear_yzmstr)
    ImageView btClearYzmstr;

    public static final int YZM_STATE_NON = 0;
    public static final int YZM_STATE_GETTING = 1;
    public static final int YZM_STATE_GETTING_OVERTIME = 2;
    LoginByPhoneViewHelper helper;

    @Override
    protected int setContentViewResId() {
        return R.layout.activity_login_by_phone;
    }


    @OnClick({R.id.tv_hqyzm, R.id.bt_login, R.id.bt_clear_yzmstr, R.id.bt_clear_phonestr})
    public void onClickView(View view) {
        switch (view.getId()) {
            case R.id.tv_hqyzm:
                getERCode();
                break;
            case R.id.bt_clear_yzmstr:
                et_yzm.setText( "" );
                break;
            case R.id.bt_clear_phonestr:
                et_phone.setText( "" );
                break;
            case R.id.bt_login:
                login();
                break;
        }
    }

    @Override
    protected void initView(@Nullable Bundle savedInstanceState) {
        super.initView( savedInstanceState );
        helper = new LoginByPhoneViewHelper( this );
        StatusBarUtil.StatusBarLightMode( this );
        et_phone.addTextChangedListener( new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (TextUtils.isEmpty( s )) {
                    btClearPhonestr.setVisibility( View.GONE );
                } else {
                    btClearPhonestr.setVisibility( View.VISIBLE );
                }

            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        } );
        et_yzm.addTextChangedListener( new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (TextUtils.isEmpty( s )) {
                    btClearYzmstr.setVisibility( View.GONE );
                } else {
                    btClearYzmstr.setVisibility( View.VISIBLE );
                }

            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        } );
        setYZMBtState( YZM_STATE_NON );

    }

    private void login() {
        String phoneStr = et_phone.getText().toString();
        if (TextUtils.isEmpty( phoneStr ) || !MTextUtil.isPhoneTel( phoneStr )) {
            showShortToast( getString( R.string.qingshuruyouxiao ) );
            return;
        }
        String erCodeStr = et_yzm.getText().toString();
        if (TextUtils.isEmpty( erCodeStr )) {
            showShortToast( getString( R.string.qingtianxieyanzhengma ) );
            return;
        }
        showDefaultProgressDialog( true );
        helper.login( phoneStr, erCodeStr );

    }

    public void setYZMBtState(int state) {
        switch (state) {
            case YZM_STATE_NON:
                resetErCodeBt();
                tv_hqyzm.setText( getString( R.string.huoquyanzhengma ) );
                break;
            case YZM_STATE_GETTING:
                resetErCodeBt();
                startCount();
                break;
            case YZM_STATE_GETTING_OVERTIME:
                resetErCodeBt();
                tv_hqyzm.setText( getString( R.string.chongxhq ) );
                break;
        }
    }


    public void resetErCodeBt() {
        tv_hqyzm.setSelected( false );
        tv_hqyzm.setEnabled( true );
        leftTime = WHOLE_TIME;
        handler.removeMessages( MSG_COUNT_TIME );
    }


    public void goMain() {
        startActivityByDefaultAnim( MainActivity.class );
    }

    public void onLoginSuccess() {
        dissmisProgressDialog();
        startActivityByDefaultAnim( MainActivity.class );
        showShortToast( getResources().getString( R.string.dlcg ) );
        finish();
    }

    private void getERCode() {
        String phoneStr = et_phone.getText().toString();
        if (TextUtils.isEmpty( phoneStr ) || !MTextUtil.isPhoneTel( phoneStr )) {
            showShortToast( getString( R.string.qingshuruyouxiao ) );
            return;
        }
        startCount();
        helper.sendERcode( phoneStr );
    }

    private void startCount() {
        tv_hqyzm.setEnabled( false );
        tv_hqyzm.setSelected( true );
        handler.sendEmptyMessage( MSG_COUNT_TIME );
    }

    @Override
    protected void onStop() {
        super.onStop();
        resetErCodeBt();

    }

    private final int MSG_COUNT_TIME = 100;
    private final int WHOLE_TIME = 60;
    private int leftTime = 60;
    private Handler handler = new Handler( new Handler.Callback() {
        @Override
        public boolean handleMessage(Message message) {
            if (message.what == MSG_COUNT_TIME) {
                if (leftTime > 0) {
                    leftTime--;
                    tv_hqyzm.setText( Html.fromHtml( getString( R.string.chongxhq_count, String.valueOf( leftTime ) ) + "s" ) );
                    handler.sendEmptyMessageDelayed( MSG_COUNT_TIME, 1000 );
                } else {
                    setYZMBtState( YZM_STATE_GETTING_OVERTIME );
                }
            }
            return false;
        }
    } );


}
